#include"Agreement.hpp"
#include"Socket.hpp"
#include<memory>
#include<iostream>
#include<unistd.h>
#include"Addr.hpp"
void UseOfClient(char* ch[])
{
    std::cout << "Useage: " << ch[0] << " ip port" << std::endl;
}


int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        UseOfClient(argv);
        exit(1);
    }

    std::unique_ptr<Socket> _sockfd = std::make_unique<TcpSocket>();
    _sockfd->CreateSocket();

    std::string _serverip = argv[1];
    uint16_t _serverport = std::stoi(argv[2]);

    Inet_Addr _addr(_serverport,_serverip);
    _sockfd->Connect(_addr);

    while(1)
    {
        std::string _buffer;
        Demd _demand;
        std::cout << "The order is X Y oper" << std::endl;
        std::cin >> _demand._x;
        std::cin >> _demand._y;
        std::cin >> _demand._oper;

        _demand.Serialize(&_buffer);
        _buffer = Agreement::Package(_buffer);

        _sockfd->Send(_buffer);
        

        _buffer.clear();
        _sockfd->Recv(&_buffer);
        _buffer = Agreement::UnPackage(_buffer);
        Result res;
        res.Deserialize(_buffer);

        std::cout << res.GetResult() << "[" << res.GetCode() << "]" << std::endl;


    }


    return 0;
}